<template>
  <Dialog title="指派服务人员" v-model="dialogVisible" width="1300px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px"/>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px"/>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="姓名" align="center" prop="name"/>
        <el-table-column label="头像" align="center" prop="pic">
          <template #default="{ row }">
            <el-image :src="row.pic" class="h-75px" @click="imagePreview(row.pic)"/>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age"/>
        <el-table-column label="性别" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="职业" align="center" prop="job">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.PEIZHEN_JOB_TYPE" :value="scope.row.job"/>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="phone"/>
        <el-table-column label="是否缴纳保证金" align="center" prop="isEarnest">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.PEIZHEN_TRUE_FALSE" :value="scope.row.isEarnest"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="120px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="passStaff( scope.row)"
              v-hasPermi="['peizhen:staff:update']"
            >
              指派
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="dialogVisible = false">关闭</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
const message = useMessage() // 消息弹窗
import {DICT_TYPE} from "@/utils/dict";
import {StaffApi, StaffVO} from '@/api/peizhen/staff'
import {HospitalApi} from '@/api/peizhen/hospital'
import {BuiessOrderApi, BuiessOrderVO} from '@/api/peizhen/buiessorder'

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const loading = ref(true) // 列表的加载中
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  phone: undefined,
  cityCode: undefined
})
const total = ref(0) // 列表的总页数
const list = ref<StaffVO>([])
const orderDetail = ref({})
defineOptions({name: 'PassStaff'})
/** 打开弹窗 */
const open = async (order) => {
  orderDetail.value = order
  dialogVisible.value = true
  // 修改时，设置数据
  debugger
  if (orderDetail.value.hospitalId) {
    formLoading.value = true
    try {
      const hospital = await HospitalApi.getHospital(orderDetail.value.hospitalId)
      queryParams.cityCode = hospital.cityCode
      getList(queryParams)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({open}) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StaffApi.getStaffPage(queryParams)
    //设置城市code
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const passStaff = async (staff) => {
  loading.value = true
  try {
    orderDetail.value.staffId = staff.id;
    orderDetail.value.staffName = staff.name
    const data = await BuiessOrderApi.updateBuiessOrderStaff(orderDetail.value)
    if (data) {
      message.success("指派成功")
    }
  } finally {
    loading.value = false
  }
}
</script>
<style scoped lang="scss">

</style>
